package com.time.timespace.domain.dto;

import com.alibaba.fastjson.annotation.JSONField;
import com.time.timespace.commons.constant.BaseConstants;
import jakarta.servlet.http.HttpServletRequest;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.apache.commons.lang3.StringUtils;

import java.util.Arrays;

@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class TokenDTO {

    @JSONField(name = "access_token")
    private String access_token;

    @JSONField(name = "expires_in")
    private Long expires_in;

    @JSONField(name = "token_type")
    private String token_type;

    /**
     * 获取token
     * @param request request
     * @return token
     */
    public static String getToken(HttpServletRequest request) {
        String token = request.getHeader("Authorization");
        if (StringUtils.isNotBlank(token)) {
            Object[] splitToken = Arrays.stream(token.split(" ")).toArray();
            if (BaseConstants.BEARER.equals(splitToken[0].toString())) {
                token = splitToken[1].toString();
            }
        }
        return token;
    }
}
